<template>
  <div class="appVersion">
    <el-row>
      <el-col :span="24">
        <dynForm :info-form="infoForm" :search-tags="searchTags">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}</el-button
            >
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        icon="el-icon-plus"
        type="primary"
        @click="switchHandle('add')"
      >{{ $t("addAppPackage") }}</el-button
      >
      <el-button
        :disabled="UpdateDisabled"
        icon="el-icon-plus"
        type="primary"
        @click="switchHandle('CreateQR')"
      >{{ $t("scanDownload") }}</el-button
      >
      <el-button
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="switchHandle('del')"
      >{{ $t("delete") }}</el-button
      >
    </div>
    <!-- 主体部分 -->
    <el-card class="box-card" shadow="never">
      <!-- 表单部分 -->
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <!--分页组件-->
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <!--表单渲染-->
    <!-- <eForm :job-status="this.job_status" /> -->
    <!-- 弹窗组件 -->
    <myDialog
      ref="myDialog"
      :form="form"
      :title="title"
      :is-disabled="isDisabled"
      @initList="initList"
    />
    <!-- 生成二维码组件 -->
    <qrDialog ref="qrDialog" :title="title" />
  </div>
</template>

<script>
import crudJob from '@/api/system/appupdate'
import eHeader from '../../components/header'
import myDialog from './module/form'
import qrDialog from './module/qrDialog'
import mySetting from '@/mixins/mySetting'
import { mapGetters } from 'vuex'
export default {
  name: 'AppUploadPage',
  components: { eHeader, qrDialog, myDialog },
  mixins: [mySetting],
  // 数据字典
  data() {
    return {
      filePath: '',
      multipleSelection: [],
      DeleteDisabled: true,
      UpdateDisabled: true,
      title: '', // 弹窗标题
      form: {},
      infoForm: {},
      searchTags: [
        { label: '关键字', prop: 'name', tag: 'DynInput' }
      ],
      isDisabled: false, // 是否禁用表单内容
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        {
          prop: 'version',
          label: this.$t('version'),
          width: '150px'
        },
        {
          prop: 'content',
          label: this.$t('updateContent')
        },
        {
          prop: 'fileName',
          label: this.$t('fileName'),
          width: '320px'
        },
        {
          prop: 'createTime',
          label: this.$t('createTime'),
          width: '180px'
        }
      ],
      tableLoading: true
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  created() {
    this.initList()
  },
  methods: {
    switchHandle(val) {
      switch (val) {
        case 'add':
          this.handleClickAdd()
          break
        case 'CreateQR':
          this.handleClickCreateQR()
          break
        case 'del':
          this.handleDel()
          break
        default:
          break
      }
    },
    // 新增数据
    handleClickAdd() {
      this.title = this.$t('addAppPackage')
      this.$refs.myDialog.openDialog()
    },
    // 二维码
    handleClickCreateQR() {
      this.title = this.$t('scanDownload')
      this.$refs.qrDialog.openDialog(this.filePath)
    },
    // 删除数据
    handleDel() {
      this.$confirm(
        this.$t('confirmDelete') +
          this.multipleSelection.length +
          this.$t('recordCount'),
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map((ele) => {
          return ele.id
        })
        crudJob.del({ ids: ids }).then((res) => {
          this.doMerge(res)
          this.setOnePage()
          this.initList()
        })
      })
    },
    // 切换分页
    getList(val) {
      this.options.loading = true
      this.listQuery = val
      this.initList()
    },
    // 初始化列表
    initList() {
      const obj = {
        ...this.listQuery,
        ...this.infoForm
      }
      crudJob.getList(this.$clearNull(obj)).then((res) => {
        this.tableData = res.data
        this.total = res.total
        this.tableLoading = false
      })
    },
    // 点击查询
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/defaultStyle.scss";
.appVersion .el-row:nth-child(1) {
  padding-bottom: 0;
}
</style>
